class TwitterController < ApplicationController
  before_filter :login_required
  #Retreive the friend activities
  def index
    @friends = current_user.friends
    
    #@friends = [@friends[0]]
    logger.info @friends.inspect
    #friend_tweet_array = @friends.collect{|f| current_user.twitter.get("/statuses/user_timeline?id=#{f[:id]}").collect{|t| {:created_at => t.created_at, :in_reply_to => t.in_reply_to_user_id}}}

    #logger.info current_user.twitter.get("/statuses/user_timeline.json?id=789420").inspect

    structure = @friends.collect do |f| 
      {:id=>f['friend_id'], 
       :screen_name => f.twitter_user['screen_name'], 
       :name => f.twitter_user['name'], 
       :count => f.twitter_user['statuses_count'], 
       :image => f.twitter_user['profile_image_url'], 
       :tweets => current_user.friend_statuses(f['friend_id']).collect do |t| 
         {:created_at => t['twitter_created_at'], :in_reply_to => t['in_reply_to_user_id']} 
        end 
      }
    end
    # 789420
    logger.info structure.inspect
    
    @profiles = structure.collect{|f| {:id=>f[:id], :screen_name=>f[:screen_name], :name=>f[:name], :count => f[:count], :image=>f[:image], :score => f[:tweets].sum{|t| (Time.now - t[:created_at])/(3600*24)} } }
    
    logger.info @profiles.inspect
  end
  
  #Retreive the friend timeline
  def tweets
    @tweets = current_user.twitter.get('/statuses/friends_timeline')
    logger.info @tweets
  end

end
